package com.abigdreamer.im.client.module.talk.cinema;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import com.abigdreamer.im.client.module.network.socket.ConnectSession;
import com.abigdreamer.im.client.module.talk.TalkWindow;
import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.util.InetAddressTool;
import com.abigdreamer.im.message.CinemaResponseMessage;
import com.abigdreamer.im.message.MessageType;


/**
 */
public class WaitingCinemaJPanel extends JPanel {

	private Box mainBox=Box.createVerticalBox();
	private JPanel myCinemaPanle;
	private JPanel friendCinemaPanle;
	private JPanel cinemaInfoPanle;
	private RegulationCinemaJPanel regulationPanel;
	private TalkWindow talkFrameParent;
	private JTabbedPane parent;
	private JButton agreeButton;
	private JButton declineButton
	;
	public WaitingCinemaJPanel(String flag){
		super();
		initPanle(flag);
		this.add(mainBox);
		this.setSize(150, 460);

	}
	
	private void initPanle(String flag){
		myCinemaPanle=new JPanel();
		friendCinemaPanle=new JPanel();
		cinemaInfoPanle=new JPanel();
		
		myCinemaPanle.setBackground(new java.awt.Color(102, 204, 255));
		friendCinemaPanle.setBackground(new java.awt.Color(102, 204, 255));
		cinemaInfoPanle.setBackground(new java.awt.Color(102, 204, 255));
	    this.setBackground(new java.awt.Color(102, 204, 255));
		
	    declineButton=new JButton("拒绝");
	    agreeButton=new JButton("同意");
	    
		JLabel myLoadingCinemaLable=new JLabel();
		JLabel friendLoadingCinemaLable=new JLabel();
		
		ImageIcon myLoadinImage=ResourcesManagement.getImageIcon("ui/cinameLoading.gif");
		myLoadingCinemaLable.setIcon(myLoadinImage);
		
		ImageIcon friendLoadingImage=ResourcesManagement.getImageIcon("ui/cinameLoading.gif");
		friendLoadingCinemaLable.setIcon(friendLoadingImage);
		

		JLabel textLable=new JLabel();
		textLable.setText("当前网络状况");
		
		JLabel cinemaWangLuoInfoLable=new JLabel();
		ImageIcon cinemaWangLuoInfoImage=ResourcesManagement.getImageIcon("ui/Info_5_Bars.png");
		cinemaWangLuoInfoLable.setIcon(cinemaWangLuoInfoImage);
		Box box1=Box.createVerticalBox();
		
		Box box=Box.createHorizontalBox();
		box.add(textLable);
		box.add(cinemaWangLuoInfoLable);
		for(int i=0;i<6;i++){
			JLabel lable=new JLabel("   ");
			box1.add(lable);
		}
		if("get".equals(flag)){
			Box agreebox=Box.createHorizontalBox();
			agreebox.add(declineButton);
			agreebox.add(agreeButton);
			
			declineButton.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	            	declineButton_MouseClicked(evt);
	            }
	        });
			
			agreeButton.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	            	agreeButton_MouseClicked(evt);
	            }
	        });
			box1.add(agreebox);
		}
	
		box1.add(box);
		cinemaInfoPanle.add(box1);
		
		
		
		myCinemaPanle.add(myLoadingCinemaLable);
		regulationPanel=new RegulationCinemaJPanel();
		regulationPanel.setMyContainer(this);
		friendCinemaPanle.add(friendLoadingCinemaLable);
		
		mainBox.add(myCinemaPanle);
		mainBox.add(regulationPanel);
		mainBox.add(friendCinemaPanle);
		mainBox.add(cinemaInfoPanle);
		
		
	}

	private void declineButton_MouseClicked(MouseEvent event){
		//---------操作界面代码-----------//
		parent.remove(this);
		TalkWindow.isViedoing=false;
		//----------操作消息----------//
		String owerQQ=talkFrameParent.owerUser.getUserID();
		String destQQ=talkFrameParent.friend.getUserID();
		CinemaResponseMessage responseMessage=new CinemaResponseMessage(owerQQ,destQQ);
		responseMessage.setCinemaResponse(MessageType.CinemaResponse_Decline);
		ConnectSession session=ConnectSession.getInstance();
		session.sendTextMessage(responseMessage);
	}
	
	private void agreeButton_MouseClicked(MouseEvent event){
       //		---------操作界面代码-----------//
		TalkWindow.isViedoing=true;
		agreeButton.setVisible(false);
		declineButton.setVisible(false);
       //		----------操作消息----------//
		String owerQQ=talkFrameParent.owerUser.getUserID();
		String destQQ=talkFrameParent.friend.getUserID();
		CinemaResponseMessage responseMessage=new CinemaResponseMessage(owerQQ,destQQ);
		responseMessage.setCinemaResponse(MessageType.CinemaResponse_Accepted);
		
		ConnectSession session=ConnectSession.getInstance();
		session.sendTextMessage(responseMessage);
		
		CinemaIPPort cinemaIPPortBean=talkFrameParent.getCinemaIPPortBean();
		cinemaIPPortBean.setGetMediaIP(InetAddressTool.getLocalIP());
		
//		ClientGetCinemaProcess getCinemaProcess=new ClientGetCinemaProcess(cinemaIPPortBean);
//		
//		getCinemaProcess.setQqTalkFrame(talkFrameParent);
//		 Thread thread=new Thread(getCinemaProcess);
//		 thread.start(); 
		
	}
	
    public void addCinemaPlayer(String flag,PlayerFrame playerPanel){
    	playerPanel.setSize(150, 150);
    	if("get".equals(flag)){
    		friendCinemaPanle.removeAll();
    		friendCinemaPanle.add(playerPanel,BorderLayout.CENTER);
    	}else if("send".equals(flag)){
    		myCinemaPanle.removeAll();
    		myCinemaPanle.add(playerPanel,BorderLayout.CENTER);
    	}
	      Runnable showOptionPane =  
		      new Runnable() { 
		        public void run() { 
		        	JTabbedPane tabbedPane=talkFrameParent.getJTabbedPane();
		        	double width=tabbedPane.getSize().getWidth();
		        	double height =tabbedPane.getSize().getHeight();
		        	System.out.println("width:"+width+"  height:"+height);
		        	talkFrameParent.pack();
		        } 
		    }; 
		    try {
				SwingUtilities.invokeAndWait(showOptionPane);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} 

    }
	
	public JTabbedPane getParent() {
		return parent;
	}

	public void setParent(JTabbedPane parent) {
		this.parent = parent;
	}

	public TalkWindow getTalkFrameParent() {
		return talkFrameParent;
	}

	public void setTalkFrameParent(TalkWindow talkFrameParent) {
		this.talkFrameParent = talkFrameParent;
	}

}
